﻿package mortal.game.view.tianDiRank
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.copyFailWin.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.tianDiRank.data.*;
    import mortal.game.view.tianDiRank.item.*;
    import mortal.game.view.tianDiRank.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TianDiRankController extends CopyControllerBase
    {
        private var _rankView:TianDiRankModule;

        public function TianDiRankController()
        {
            return;
        }// end function

        override protected function initView() : IView
        {
            if (!this._rankView)
            {
                this._rankView = new TianDiRankModule();
                this._rankView.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._rankView.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return this._rankView;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.NavbarButtonUIRowInited, this.initHd, true);
            EventUtil.addSelfEvent(EventName.TianDiRankOpenModule, this.onOpenM, true);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.initHd);
            EventUtil.addSelfEvent(EventName.TianDiRankGetListInfo, this.onGetListInfo, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankGetDiMainInfo, this.onUpdateShowList, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankCopyCDTime, this.onCDTimeBack, true);
            EventUtil.addSelfEvent(EventName.TianDiRankPosAttListCk, this.onShowPosAttrPanel, true);
            EventUtil.addSelfEvent(EventName.TianDiRankAccelerationCDCk, this.onAcceleHd, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankClearCopyCDSucc, this.onClearCDSucc, true);
            EventUtil.addSelfEvent(EventName.TianDiRankCkToShowPanel, this.onShowEnterPanelReq, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankGetPosDetail, this.onEnterPanelBack, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankJobBusy, this.onUpdateEnterPanelCDBack, true);
            EventUtil.addSelfEvent(EventName.TianDiRankEnterCopy, this.onEnterCopyReq, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankCopyInfo, this.onUpdateCopyInfo, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankBeReplace, this.showReplaceIcon, true);
            EventUtil.addSelfEvent(EventName.TianDiRankShowReplaceAlert, this.onShowAlert, true);
            EventUtil.addNetEvent(ServerCommand.TianDiRankChallengeResultInfo, this.onUpdateShowResult, true);
            EventUtil.addSelfEvent(EventName.TianDiRankLeaveCopy, this.onLeaveCopy, true);
            EventUtil.addNetEvent(ServerCommand.TitleUpdateTitle, this.onUpdateShowResultTitle, true);
            EventUtil.addSelfEvent(EventName.TianDiRankUseTitle, this.onUseTitle, true);
            return;
        }// end function

        private function addEvts(param1:Boolean) : void
        {
            return;
        }// end function

        protected function onViewShow(event:Event) : void
        {
            this.addEvts(true);
            return;
        }// end function

        protected function onViewHide(event:Event) : void
        {
            this.addEvts(false);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            if (TianDiRankUtil.checkLevelEnougth(47))
            {
                TianDiRankIcon.instance.show();
            }
            return;
        }// end function

        private function onOpenM(param1:Object) : void
        {
            if (!TianDiRankUtil.checkLevelEnougth(47))
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(71977, GameDefConfig.instance.getSystemOpenLevel(47)));
                return;
            }
            if (this.view.isHide)
            {
                GameManager.instance.popupView(this.view);
            }
            return;
        }// end function

        private function onGetListInfo(event:DataEvent) : void
        {
            var _loc_2:* = Cache.instance.tianDi.isDiRankFlag;
            if (_loc_2)
            {
                GameProxy.copy.getEarthPlacardInfo();
            }
            else
            {
                GameProxy.copy.getHeavenPlacardInfo();
            }
            return;
        }// end function

        private function onUpdateShowList(param1:Object) : void
        {
            var _loc_4:Boolean = false;
            var _loc_2:* = Cache.instance.tianDi.isDiRankFlag;
            if (this._rankView && this._rankView.curPanel)
            {
                (this._rankView.curPanel as TianDiRankDiShowPanel).updateListInfo(_loc_2);
            }
            if (TianDiRankUtil.isCkShowPos)
            {
                TianDiRankUtil.isCkShowPos = false;
                _loc_4 = Cache.instance.tianDi.isDiRankFlag;
                TianDiRankPosDetail.instance.show();
                TianDiRankPosDetail.instance.updateCells(_loc_4);
            }
            var _loc_3:* = Cache.instance.tianDi.openIndex;
            if (_loc_3 && _loc_3.length > 1)
            {
                _loc_3 = _loc_3.concat();
                Dispatcher.dispatchEvent(new DataEvent(EventName.TianDiRankCkToShowPanel, {pos:_loc_3[1]}));
                Cache.instance.tianDi.openIndex = null;
            }
            return;
        }// end function

        private function onShowEnterPanelReq(param1:Object) : void
        {
            var _loc_2:* = param1.data;
            var _loc_3:* = _loc_2.pos;
            TianDiEnterPanel.instance.show();
            TianDiEnterPanel.instance.updateBaseInfo(_loc_3);
            var _loc_4:* = TianDiRankUtil.getCopyCfgByPos(_loc_3);
            if (_loc_4 == null)
            {
                return;
            }
            var _loc_5:* = _loc_4.id;
            GameProxy.copy.getPlacardDetailInfo(_loc_5);
            return;
        }// end function

        private function onEnterPanelBack(param1:Object) : void
        {
            var _loc_2:SPlacardDetailInfo = null;
            if (TianDiEnterPanel.instance.isDisposed == false)
            {
                _loc_2 = Cache.instance.tianDi.detail;
                TianDiEnterPanel.instance.updateSeverInfo(_loc_2);
            }
            return;
        }// end function

        private function onUpdateEnterPanelCDBack(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (TianDiEnterPanel.instance.isDisposed == false)
            {
                _loc_2 = Cache.instance.tianDi.jobBusy.time;
                TianDiEnterPanel.instance.updateCDTime(_loc_2);
            }
            return;
        }// end function

        private function onCDTimeBack(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.tianDi.isDiRankFlag;
            if (this._rankView && this._rankView.curPanel)
            {
                (this._rankView.curPanel as TianDiRankDiShowPanel).updateCDBtn(_loc_2);
            }
            return;
        }// end function

        private function onUpdateShowResult(param1:Object) : void
        {
            var _loc_3:String = null;
            var _loc_2:* = Cache.instance.tianDi.resultInfo;
            if (_loc_2.result == 1)
            {
                TianDiRankPassWin.instance.show();
                TianDiRankPassWin.instance.updateResult(_loc_2);
            }
            else
            {
                _loc_3 = TianDiRankUtil.getBossNameByPId(_loc_2.placardConfigId);
                CopyFailBaseWin.instance.show();
                CopyFailBaseWin.instance.tianDiShow("" + _loc_3);
                CopyFailBaseWin.instance.tipsImageUrl = ImagesConst.copyFailBaseWinTxt_1;
            }
            return;
        }// end function

        private function onUpdateShowResultTitle(param1:Object) : void
        {
            var _loc_2:* = param1 as SPlayerTitle;
            var _loc_3:* = _loc_2.updateType;
            var _loc_4:* = _loc_2.title;
            var _loc_5:* = TitleConfig.instance.getInfoById(_loc_4.titleCode);
            var _loc_6:* = new TitleDataInfo();
            _loc_6.titleData = _loc_5;
            if (TianDiRankUtil.isTianDianTitle(_loc_6))
            {
                TianDiRankPassWin.instance.updateTitle(_loc_6);
            }
            return;
        }// end function

        private function onUseTitle(param1:Object) : void
        {
            var _loc_2:* = param1.data as TitleDataInfo;
            if (_loc_2)
            {
                GameProxy.titleProxy.enableTitle(_loc_2.titleData.titleId);
            }
            return;
        }// end function

        private function onLeaveCopy(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CopyLeave));
            return;
        }// end function

        private function onShowPosAttrPanel(event:DataEvent) : void
        {
            this.onGetListInfo(null);
            return;
        }// end function

        private function onAcceleHd(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:String = null;
            var _loc_5:String = null;
            var _loc_2:* = this.getAllMoney();
            if (_loc_2 > 0)
            {
                _loc_3 = 1;
                _loc_4 = GameDefConfig.instance.getEPrictUnitName(EPriceUnit._EPriceUnitGold);
                _loc_5 = Language.getStringByParam(71978, _loc_2, _loc_4);
                TodayNotTipUtil.toDayNotTip(TodayNoTipsConst.TianDiRankAcceleration, this.getCallback, _loc_5, null, Alert.OK | Alert.CANCEL);
                Cache.instance.tianDi.cdCkIndex = TianDiRankUtil.getIndex();
            }
            return;
        }// end function

        private function getAllMoney() : int
        {
            var _loc_1:SecTimerView = null;
            var _loc_2:int = 0;
            var _loc_3:Number = NaN;
            var _loc_4:int = 0;
            if (this._rankView && this._rankView.curPanel)
            {
                _loc_1 = (this._rankView.curPanel as TianDiRankDiShowPanel).leftTime;
                if (_loc_1 && _loc_1.secTimerViewData)
                {
                    _loc_2 = _loc_1.secTimerViewData.leftTime;
                    _loc_3 = TianDiRankUtil.MinPerCoin;
                    _loc_4 = Math.ceil(_loc_2 / 60 / _loc_3);
                    return _loc_4;
                }
            }
            return 0;
        }// end function

        private function getCallback() : void
        {
            var _loc_1:* = this.getAllMoney();
            var _loc_2:* = ConstConfig.instance.getValueByName("EarthPlacardCopyCode");
            if (Cache.instance.tianDi.cdCkIndex != 0)
            {
                _loc_2 = ConstConfig.instance.getValueByName("HeavenPlacardCopyCode");
            }
            GameProxy.copy.clearEnterCopyDiCd(_loc_2, _loc_1);
            return;
        }// end function

        private function onClearCDSucc(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.tianDi.isDiRankFlag;
            if (this._rankView && this._rankView.curPanel)
            {
                (this._rankView.curPanel as TianDiRankDiShowPanel).updateCDBtn(_loc_2);
            }
            return;
        }// end function

        private function onEnterCopyReq(event:DataEvent) : void
        {
            var _loc_2:* = event.data.pos;
            var _loc_3:* = TianDiRankUtil.getCopyCfgByPos(_loc_2);
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = _loc_3.id;
            TianDiInfoPanel.instance.updateCopyPos(_loc_2);
            CopyUtil.copyCheck(this.enterAfterCheck, _loc_4);
            return;
        }// end function

        private function enterAfterCheck(param1:int) : void
        {
            GameProxy.copy.enterPlacardCopy(param1);
            return;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            TianDiRankUtil.isInCopy = true;
            if (TianDiEnterPanel.hasInstance())
            {
                TianDiEnterPanel.instance.hide();
            }
            if (TianDiRankPassWin.hasInstance())
            {
                TianDiRankPassWin.instance.hide();
            }
            if (this._view)
            {
                this.view.hide();
            }
            Cache.instance.tianDi.realDiRankFlag = Cache.instance.tianDi.isDiRankFlag;
            return;
        }// end function

        private function onUpdateCopyInfo(param1:Object) : void
        {
            TianDiInfoPanel.instance.show();
            TianDiInfoPanel.instance.updateUpdateInfo();
            var _loc_2:* = Cache.instance.tianDi.copyInfo;
            TianDiInfoPanel.instance.updateCopyTime(_loc_2.time);
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            TianDiRankUtil.isInCopy = false;
            if (TianDiEnterPanel.hasInstance())
            {
                TianDiEnterPanel.instance.hide();
            }
            if (TianDiInfoPanel.hasInstance())
            {
                TianDiInfoPanel.instance.hide();
            }
            var _loc_1:* = Cache.instance.tianDi.resultInfo;
            if (_loc_1 && _loc_1.result != 1)
            {
                this.onUpdateShowResult(null);
            }
            return;
        }// end function

        private function showReplaceIcon(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.tianDi.replace;
            if (_loc_2 == null)
            {
                return;
            }
            var _loc_3:* = TianDiRankConfig.instance.isDiById(_loc_2.placardConfigId);
            TianDiRankHintIcon.instance.reSetIcon(_loc_3 ? (0) : (1));
            TianDiRankHintIcon.instance.info = _loc_2;
            TianDiRankHintIcon.instance.show();
            return;
        }// end function

        private function onShowAlert(event:DataEvent) : void
        {
            var _loc_2:* = TianDiRankUtil.getReplaceInfo();
            var _loc_3:* = Language.getStringByParam(71979, _loc_2[0]);
            var _loc_4:* = _loc_2[1];
            var _loc_5:String = "";
            _loc_3 = HTMLUtil.addColor(_loc_3, "#ff0000");
            _loc_4 = HTMLUtil.addColor(_loc_4, "#00ff00");
            _loc_5 = HTMLUtil.addColor(_loc_5, "#ff0000");
            var _loc_6:* = Language.getStringByParam(71980, _loc_3, _loc_4, _loc_5);
            Alert.yesLabel = "" + Language.getString(71981);
            Alert.buttonWidth = 65;
            Alert.show(_loc_6, null, Alert.YES | Alert.CANCEL, null, this.alertFun);
            return;
        }// end function

        private function alertFun(param1:int) : void
        {
            TianDiRankHintIcon.instance.hide();
            if (Alert.YES == param1)
            {
                this.onOpenM(null);
            }
            return;
        }// end function

    }
}
